/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controlador;

import comu.Configuracio;
import controlador.autogenerats.TractamentJpaController;
import entity.Tractament;
import entity.Produccio;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
 *
 * @author David
 */
public class TractamentControler extends TractamentJpaController{

    public TractamentControler() {
        super(Persistence.createEntityManagerFactory(Configuracio.PERSISTANCE_UNIT));
    }
    
    public List<Tractament> findTractamentEntitiesByProduccio(Produccio produccio) {
        EntityManager em = getEntityManager();
        try{
            Query q = em.createNativeQuery("select * from tractament where `produccio_id` LIKE "+ produccio.getId(), Tractament.class);
            Iterator it = q.getResultList().iterator();
            ArrayList<Tractament> llista = new ArrayList<Tractament>();
            while(it.hasNext()){
                llista.add((Tractament) it.next());
            }
            return llista;
        } finally {
            em.close();
        }
    }
    public List<Tractament> findLastTractamentEntityByProduccio(Produccio produccio) {
        EntityManager em = getEntityManager();
        try{
            Query q = em.createNativeQuery("select * from tractament where `produccio_id` LIKE " + produccio.getId()+ " order by `data_tractament` desc limit 1", Tractament.class);
            Iterator it = q.getResultList().iterator();
            ArrayList<Tractament> llista = new ArrayList<Tractament>();
            while(it.hasNext()){
                llista.add((Tractament) it.next());
            }
            return llista;
        } finally {
            em.close();
        }
    }
}
